package pl.edu.mimuw.irs.rcp.controller.editor.workflow.command;

import org.eclipse.gef.commands.Command;

import pl.edu.mimuw.irs.core.model.design.net.IRSNode;
import pl.edu.mimuw.irs.rcp.controller.layout.StructuralLayout;

public abstract class IRSNodeCommand extends Command {

	// *** Attributes

	private IRSNode node;
	public IRSNode getNode() {
		return node;
	}
	
	// *** Constructors
	
	public IRSNodeCommand(IRSNode node) {
		this.node = node;
	}
	
	// *** Methods
	
	@Override
	public final void execute() {
		performExecute();
		super.execute();
		new StructuralLayout(getRootNode(getNode()).getExpandedNodes()).layout();
	}

	protected abstract void performExecute();
	
	@Override
	public final void undo() {
		performUndo();
		super.undo();
		new StructuralLayout(getRootNode(getNode()).getExpandedNodes()).layout();
	}

	protected abstract void performUndo();
	
	private IRSNode getRootNode(IRSNode node) {
		if (node.getParent() == null) return node;
		else return getRootNode(node.getParent());
	}
}
